package com.cryann.mqttserver.config;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @author cRyann
 * @date 2018/12/5 0005 10:13
 * @description:
 */
@Component
@Qualifier("nettyEncoder")
@ChannelHandler.Sharable
public class NettyEncoder extends MessageToByteEncoder<Object> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) {
        byte[] bytes = new byte[0];
        if ( msg instanceof String ) {
            bytes = ((String) msg).getBytes();
        }
        out.writeBytes(bytes);
    }
}
